/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ai;

import comm.Messenger;
import java.util.Date;
import world.Friend;
import world.World;

/**
 *
 * @author dheath
 */
public class Stupid implements Intelligence
{

    private int botId;
    private World world;
    private Messenger msgr;
    
    private double baseAngle;
    private boolean turning;
    private long lastTime;

    public Stupid(int botId)
    {
        this.botId = botId;
        baseAngle = 0.0;
        turning = true;
        lastTime = 0;
    }

    public void setWorld(World world)
    {
        this.world = world;
    }

    public void setMessenger(Messenger msgr)
    {
        this.msgr = msgr;
    }

    public void NextMove()
    {
        Date now = new Date();
        if (now.getTime() - lastTime > 2000)
        {
            Friend me = world.getFriends().get(botId);

            if (baseAngle == 0.0)
            {
                speed(.4);
            }

            if (turning)
            {
                if (Math.abs(baseAngle - me.getAngle()) >= .9)
                {
                    turning = false;
                    angvel(0.0);
                    speed(.8);
                } else
                {
                    angvel(0.7);
                }
            } else
            {

                //System.out.println()
                double wall = 800.0/2.0 - 6.0;
                
                if (Math.abs(me.getVx()) <= .07 || Math.abs(me.getVy()) <= .07 
                        || me.getLocation().getX() < -1*wall || me.getLocation().getX() > wall || me.getLocation().getY() < -1*wall || me.getLocation().getY() > wall)
                {
                    angvel(1.0);
                    speed(-0.1);
                    turning = true;
                    baseAngle = me.getAngle();
                } else
                {
                    speed(.8);
                }
            }
            lastTime = now.getTime();
        }
        shoot();

    }

    private void shoot()
    {
        msgr.sendCommand("shoot " + botId);
    }

    private void speed(double v)
    {
        msgr.sendCommand("speed " + botId + " " + v);
    }

    private void angvel(double v)
    {
        msgr.sendCommand("angvel " + botId + " " + v);
    }
}
